﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureInterop
{
	public class CloudBlobHelper
	{
		#region Public static methods
		public static string BuildConnectString(string appName, string accountKey, string protocol = "http")
		{
			return string.Format("AccountName={0};AccountKey={1};DefaultEndpointsProtocol={2};",
				appName.ToLower(),
				accountKey,
				protocol.ToLower()
			).TrimEnd(new char[] { ';' }); // ensure not trailing ; in conn str
		}

		public static CloudBlobClient GetClient(CloudStorageAccount acct)
		{
			return acct.CreateCloudBlobClient();
		}

		public static string GetName(IListBlobItem blobItem)
		{
			if (blobItem.GetType().Equals(typeof(CloudBlobDirectory)))
				return GetDirectoryName(blobItem);
			else
				return GetBlobName(blobItem);
		}
		#endregion

		#region Protected methods
		protected static string GetBlobName(IListBlobItem blobItem)
		{
			System.Diagnostics.Debug.Assert(null != blobItem.Parent);
			return blobItem.Uri.ToString().Replace(blobItem.Parent.Uri.ToString(), string.Empty);
		}

		protected static string GetDirectoryName(IListBlobItem blobItem)
		{
			string dirName = GetBlobName(blobItem);
			return dirName.Replace("/", string.Empty);
		}
		#endregion

	}
}
